package com.sixth.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixth.dao.BranchDao;
import com.sixth.dao.GoodsDao;
import com.sixth.dao.StaffDao;
import com.sixth.entity.*;
import com.sixth.service.BranchService;
import com.sixth.util.CommonResult;
import com.sixth.util.TableLayUi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: project01
 * @description:
 * @author: 高振杨
 * @create: 2021-12-17 19:04
 **/
@Service(value = "branchservice")
public class BranchServiceImpl implements BranchService {

    @Autowired
    private BranchDao dao;

    @Autowired
    private HttpSession session;

    @Autowired
    private GoodsDao goodsdao;

    @Autowired
    private StaffDao staffDao;

    public CommonResult findBranchAll() {
        List<Branch> branches=dao.selectAllbranch();
        return new CommonResult(2000,"查询成功",branches);
    }

    public CommonResult getOneBranch(Integer branchid) {
        List<Branch> oneBranch = dao.getOneBranch(branchid);
        if (oneBranch.size()>0) {
            return CommonResult.fail("分店编号重复，请重新输入！");
        }
        return CommonResult.success();
    }

    @Override
    public CommonResult getBranchName(String branchname) {
        List<Branch> branches = dao.getBranchName(branchname);
        if (branches.size() > 0) {
            return CommonResult.fail("分店名重复，请重新输入！");
        }
        return CommonResult.success();
    }

    public TableLayUi findEmpByCondition(int page, int limit, String branchname)  {
        PageHelper.startPage(page, limit); //开启PageHelper分页
        List<Branch> branches = dao.selectEmpByCondition(branchname);
        PageInfo<Branch> pageInfo = new PageInfo<Branch>(branches);
        //pageInfo.getTotal:获取的为表中总条数
        //pageInfo.getList():获取的为当前页的记录数据
        return new TableLayUi(0, "", pageInfo.getTotal(), pageInfo.getList());
    }

    //新增和修改修改店铺
    public CommonResult addOrUpdateBranch(Branch branch) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer st = staff.getStaffid();
        Integer i;
        if (branch.getId() != null) {
            if (branch.getStaffid()!=null){
                i = dao.updateBranch(branch);
            }else {
                i = dao.updateBranch1(branch);
            }
        } else {
            i = dao.addBranch(branch);
            //新增店铺的时候，分配店长
            Integer staffid = branch.getStaffid();
            System.out.println(staffid);
            //查询该员工之前的分店，部门，职位，
            Staff staffById = staffDao.getStaffById(staffid);
            Integer brid = branch.getBranchid();
            staffDao.updateBranchStaff(staffid, brid);
            //从员工表中调店长要有记录，新增到店长变动信息表中
            Shopkeeperchange sc = new Shopkeeperchange();
            sc.setStaffid(staffid);
            sc.setBranchid(staffById.getBranchid());
            sc.setDeptid(staffById.getDeptid());
            sc.setRoleid(staffById.getRoleid());
            sc.setNewbranchid(branch.getBranchid());
            sc.setNewdeptid(1);
            sc.setNewroleid(1);
            sc.setTransfertime(new Date());
            sc.setTransferper(st);
            dao.addShopkeeperChange(sc);
            /**
             * 新增店铺时，把所有商品都加到分店中去
             */
            Integer branchid = branch.getBranchid();
            List<Goods> goods = goodsdao.getAllGoodsInformation();
            ArrayList<Integer> list = new ArrayList<Integer>();
            for (Goods good : goods) {
                list.add(good.getGoodsid());
            }
            i = dao.insertBranchAndGoods(branchid, list);
        }
        CommonResult commonResult;
        if (i > 0) {
            commonResult = CommonResult.success(null, "操作成功");
        } else {
            commonResult = CommonResult.fail("操作失败");
        }
        return commonResult;
    }

    //调离店长
    public CommonResult DZTransfer(BranchTransfer branchTransfer) {

        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        //调走店长
        dao.DZTransfer(branchTransfer);
        //把分店表中的店长id清空
        dao.updateBranchStaff(branchTransfer);
        //把调走店长这条记录增到记录表中
        Shopkeeperchange sc = new Shopkeeperchange();

        sc.setStaffid(branchTransfer.getOldstaffid());

        sc.setBranchid(branchTransfer.getBranchid());
        sc.setDeptid(branchTransfer.getOldroleid());

        sc.setRoleid(branchTransfer.getOldroleid());
        sc.setNewbranchid(branchTransfer.getNewbranchid());
        sc.setNewdeptid(branchTransfer.getNewroleid());
        sc.setNewroleid(branchTransfer.getNewroleid());
        sc.setTransfertime(new Date());
        sc.setTransferper(staffid);
        System.out.println("sc**********"+sc);
        Integer i = dao.addShopkeeperChange(sc);
        CommonResult commonResult;
        if (i > 0) {
            commonResult = CommonResult.success(null, "调离成功");
        } else {
            commonResult = CommonResult.fail("调离失败");
        }
        return commonResult;
    }

    //修改店长
    public CommonResult DZUpdate(Integer branchid,Integer staffid) {

        Staff staff = (Staff) session.getAttribute("staff");
        Integer st = staff.getStaffid();
        //查询该员工之前的分店，部门，职位，
        Staff staffById = staffDao.getStaffById(staffid);
        //修改员工表内的门店编号，部门编号，职位
        dao.updateStaff(branchid, staffid);
        //修改分店表中的店长id
        dao.updateBranchSt(branchid, staffid);
        //从员工表中调店长要有记录，新增到店长变动信息表中
        Shopkeeperchange sc = new Shopkeeperchange();
        sc.setStaffid(staffid);
        sc.setBranchid(staffById.getBranchid());
        sc.setDeptid(staffById.getDeptid());
        sc.setRoleid(staffById.getRoleid());
        sc.setNewbranchid(branchid);
        sc.setNewdeptid(1);
        sc.setNewroleid(1);
        sc.setTransfertime(new Date());
        sc.setTransferper(st);
        Integer i = dao.addShopkeeperChange(sc);
        CommonResult commonResult;
        if (i > 0) {
            commonResult = CommonResult.success(null, "修改成功");
        } else {
            commonResult = CommonResult.fail("修改失败");
        }
        return commonResult;
    }

    public CommonResult updateState(Integer id, Integer state) {
        int i = dao.updateState(id, state);

        if(i>0){
            return new CommonResult(2000,"修改成功",null);
        }else{
            return new CommonResult(5000,"修改失败",null);
        }

    }

    @Override
    public CommonResult delete(Integer id) {
        int row= dao.deleteByPrimaryKey(id);
        if(row>0){
            return new CommonResult(2000,"删除成功",null);
        }else{
            return new CommonResult(5000,"删除失败",null);
        }
    }
}
